package com.test.webdemo.servlet;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TrackingInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        RequestTracker.logStep("4. Interceptor.preHandle()");
        RequestTracker.log("Handler对象: " + handler.getClass().getName());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) {
        RequestTracker.logStep("9. Interceptor.postHandle()");
        if (modelAndView != null) {
            RequestTracker.log("视图名称: " + modelAndView.getViewName());
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) {
        RequestTracker.logStep("10. Interceptor.afterCompletion()");
        if (ex != null) {
            RequestTracker.log("处理过程中发生异常: " + ex.getClass().getName());
        }
    }
}
